﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using DirectUI.Mobile.UI;
namespace DirectUI.Mobile.UI.Controls
{

    public class ModalAnimateProgress:DirectUIPanel 
    {
       private AnimateProgress ap;
       private static ModalAnimateProgress instance;
       public static ModalAnimateProgress Instance
       {
           get
           {
               if (instance == null)
               {
                   instance= new ModalAnimateProgress();
               }
               return instance;
           }
       }
        public ModalAnimateProgress()
        {
             ap = new AnimateProgress();
            ap.Left = 100;
         
            Bounds = new Rectangle(0, 100, 240, 100);
            this.Controls.Add(ap);
            this.Name = Guid.NewGuid().ToString();
        }
        public void Start()
        {
            if (this.Parent != null)
            {
                return;
            }
            ap.StartAnimation(42, 200, -1);
            this.ShowModal(ShowTransition.None);
        }
        public void Stop()
        {
            if (this.Parent == null)
            {
                return;
            }
            ap.StopAnimation();

            Close();
        }
    }
   

        public enum FrameLayouts
        {
            Horisontal = 0,
            Vertical = 1
        }

        /// <summary>
        /// Summary description for AnimateCtl.
        /// </summary>
        public class AnimateProgress :DirectUIControl
        {
            private Bitmap bitmap;
            private int frameCount;
            private int frameWidth = 42;
            private int frameHeight;
            private Graphics graphics;
            private int currentFrame = 0;
            private int loopCount = 12;
            private int loopCounter = -1;

            private Timer fTimer;


            public Bitmap Bitmap
            {
                get { return bitmap; }
                set
                {
                    bitmap = value;
                }

            }

            public AnimateProgress()
            {
                //Cache the Graphics object
                graphics = this.CreateGraphics();
                //Instanciate the Timer
                fTimer = new Timer();
                //Hook up to the Timer's Tick event
                fTimer.Tick += new System.EventHandler(this.timer1_Tick);
                bitmap = Resource.Loading;
            }

            private void timer1_Tick(object sender, System.EventArgs e)
            {
                //if (loopCount == -1) //loop continuosly
                //{
                //    this.DrawFrame();
                //}
                //else
                //{
                //    if (loopCount == loopCounter) //stop the animation
                //        fTimer.Enabled = false;
                //    else
                //        this.DrawFrame();

                //}\
                this.Invalidate();
            }

            public override void OnPaint(DirectUIPaintEventArgs e)
            {
                //base.OnPaint(e);
                graphics = e.Graphics;
                DrawFrame(e);

            }
            public void StartAnimation(int frWidth, int DelayInterval, int LoopCount)
            {

                frameWidth = frWidth;
                //How many times to loop
                loopCount = LoopCount;
                //Reset loop counter
                loopCounter = 0;
                //Calculate the frameCount
                frameCount = bitmap.Width / frameWidth;
                frameHeight = bitmap.Height;
                //Resize the control
             //   this.Size = new Size(frameWidth, frameHeight);
                this.Height = frameHeight;
                this.Width = frWidth;
                //Assign delay interval to the timer
                fTimer.Interval = DelayInterval;
                //Start the timer
                 fTimer.Enabled = true;
            }


            public void StopAnimation()
            {

                fTimer.Enabled = false;
            }

            private void DrawFrame(DirectUIPaintEventArgs pe)
            {
                if (currentFrame < frameCount - 1)
                {
                    currentFrame++;
                }
                else
                {
                    //increment the loopCounter
                    loopCounter++;
                    currentFrame = 0;
                }
                Draw(currentFrame,pe);
            }


            private void Draw(int iframe,DirectUIPaintEventArgs pe)
            {
                //Calculate the left location of the drawing frame
                int XLocation = iframe * frameWidth;

                Rectangle rect = new Rectangle(XLocation, 0, frameWidth, frameHeight);
                ImageAttributes ia = new ImageAttributes();
                //  ia.SetColorKey(Color.Fuchsia, Color.Fuchsia);
                ia.SetColorKey(bitmap.GetPixel(0, 0), bitmap.GetPixel(0, 0));
                //Draw image
            //   pe.Graphics.DrawImage(bitmap, pe.ControlBounds.X, pe.ControlBounds.Y, rect, ia);

                pe.Graphics.DrawImage(bitmap, pe.ControlBounds, rect.X, rect.Y, rect.Width, rect.Height, GraphicsUnit.Pixel, ia);
               
                //IntPtr hdcDst = pe.Graphics.GetHdc();
                //DirectUI.Mobile.UI.Drawing.GdiPlus.GdiExt.AlphaBlendImage(hdcDst, bitmap, pe.ControlBounds, rect, 255, false);
                //pe.Graphics.ReleaseHdc(hdcDst);
            }



        }
    

}
